#!/usr/bin/env python

import sys
import os.path
import time

import tsdb

DB = "/data/snmp/test_import"

type_map = {
    'ifHCInOctets': tsdb.Counter64,
    'ifHCOutOctets': tsdb.Counter64,
    'ifInOctets': tsdb.Counter32,
    'ifOutOctets': tsdb.Counter32,
    'sysUpTimeInstance': tsdb.Counter32
}

if not os.path.exists(os.path.join(DB,"TSDB")):
    db = tsdb.TSDB.create(DB, tsdb.EpochWeekMapper)
#    db = tsdb.TSDB.create(DB, tsdb.YYYYMMChunkMapper)
else:
    db = tsdb.TSDB(DB)

def main():
    for arg in sys.argv[1:]:
        (rtr,out) = os.path.basename(arg).split(".")
        print rtr
    
        if rtr not in db.list_sets():
            set = db.add_set(rtr)
        else:
            set = db.get_set(rtr)
    
        vartab = {}
        rows = 0
        begin = time.time()
        f = open(arg, "r")
        for line in f:
            #        print line
            line = line.strip()
            if line == '':
                continue
    
            rows += 1
    
            (tstamp, var, val) = line.split()
            tstamp = int(tstamp)
            val = int(val)
    
            type = type_map[var.split('.')[0]]
    
            if var not in vartab:
                if var not in set.list_vars():
                    print "CREATE ", var
                    vartab[var] = set.add_var(var, type, 30)
                else:
                    vartab[var] = set.get_var(var, use_mmap=True)
    
            new_data = type(tstamp, tsdb.ROW_VALID, val)
#            old_data = vartab[var].select(tstamp)
    
#            if old_data.flags & tsdb.ROW_VALID:
#                pass
                #print "warning: overwriting %s with %s" % (old_data, new_data)

            #def insert(var):
            #    vartab[var].insert(new_data)

            #insert(var)
            vartab[var].insert(new_data)
    
        for var in vartab:
            vartab[var].close()
    
        print "imported %d rows in %f seconds" % (rows, time.time() - begin)


if __name__ == '__main__':
    main()
#    1/0
#    import hotshot, hotshot.stats
#    prof = hotshot.Profile('fooprof')
#    prof.runcall(main)
#    prof.close()
#    stats = hotshot.stats.load('fooprof')
#    stats.strip_dirs()
#    stats.sort_stats('time', 'calls')
#    stats.print_stats()


